﻿using System;
using com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx;
using UnityEngine;
using Yoozoo.UI.YGUI;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientation.Adapter
{
    
    [Serializable]
    public class YTableViewAdapterConfig
    {
        
        public float cellSpacing;
        public TextAnchor childAlignment;
        public int direction;
        
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            YTableViewAdapterConfig other = (YTableViewAdapterConfig)obj;
            // Compare spriteName properties
            if (Math.Abs(cellSpacing - other.cellSpacing) > 0.01f || childAlignment != other.childAlignment || direction != other.direction)
            {
                return false;
            }

            return true;
        }
        
        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + cellSpacing.GetHashCode();
            hash = hash * 23 + childAlignment.GetHashCode();
            hash = hash * 23 + direction.GetHashCode();
            return hash;
        }
    }
    
    [Serializable]
    public class YTableViewAdapter: AdaptBase<YTableViewAdapterConfig, YTableView>
    {
        
        protected override void CopyProperty(YTableViewAdapterConfig config)
        {
            base.CopyProperty(config);
            config.cellSpacing = mComponent.CellSpacing;
            config.childAlignment = mComponent.ChildAlignment;
            config.direction = (int)mComponent.direction;
        }

        protected override void ApplyConfig(YTableViewAdapterConfig config)
        {
            base.ApplyConfig(config);

            var pos = mComponent.normalizedPosition;
            var percent = mComponent.direction == YScrollRect.Direction.Horizontal ? pos.x : pos.y;
            // var index = Mathf.RoundToInt(percent * mComponent.totalCount);
            // index = Mathf.Clamp(index, 0, mComponent.totalCount - 1);
            mComponent.CellSpacing = config.cellSpacing;
            mComponent.ChildAlignment = (TextAnchor)config.childAlignment;
            mComponent.direction = (YScrollRect.Direction)config.direction;
#if UNITY_EDITOR
                if (!UnityEditor.EditorApplication.isUpdating)
                {
                    if (mComponent.totalCount > 0)
                    {
                        mComponent.CheckLayout();
                        mComponent.ReloadData();
                    }
                }
#else
                if (mComponent.totalCount > 0)
                {
                    mComponent.CheckLayout();
                    mComponent.ReloadData();
                }
#endif
            var x = mComponent.direction == YScrollRect.Direction.Horizontal ? percent : 0;
            var y = mComponent.direction == YScrollRect.Direction.Horizontal ? 0 : percent;
            // 设置TableView.normalizedPosition 只会对当前方向生效，所以需要把其余方向的偏移清零
            mComponent.content.anchoredPosition = Vector2.zero;
            mComponent.normalizedPosition = new Vector2(x, y);
            // mComponent.ScrollToCell(index, 1000);
        }

        public override int Order => (int)AdapterExecuteOrder.ListView;
    }

   
}